<?php

/**
 * axis actions.
 *
 * @package    WacStorehouse
 * @subpackage axis
 * @author     JianBinBi <prince.bi@gmail.com>
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class axisActions extends sfActions {
    /**
     * Executes an application defined process prior to execution of this sfAction object.
     *
     * By default, this method is empty.
     */
    public function preExecute() {
        $this->moduleName = sfContext::getInstance()->getRequest()->getParameter("module");
        $moduleName = $this->moduleName;
        $this->mainModuleTable = Doctrine::getTable(WacTable::$$moduleName);

        parent::preExecute();
    }

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {
        ;
    }

    /*
   * @return list data array
    */
    public function executeGetList(sfWebRequest $request) {
        if ($this->getRequest()->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $dbTable1 = Doctrine::getTable(WacTable::$axis);
        $jqGridDataHelper = JqGridDataHelper::getInstance();

        if($request->hasParameter(JqGridDataHelper::$KEY_SEARCH_FIELD) || $request->hasParameter(JqGridDataHelper::$KEY_SIDX)) {
            $arrParam = array();

            if($request->hasParameter(JqGridDataHelper::$KEY_SEARCH_FIELD)) {
                $arrParam['andWhere'] = array(
                        $jqGridDataHelper->getCondMapStr($request->getParameter(JqGridDataHelper::$KEY_SEARCH_OPER), "t1.".$request->getParameter(JqGridDataHelper::$KEY_SEARCH_FIELD), $request->getParameter(JqGridDataHelper::$KEY_SEARCH_STRING))
                );
            }

            if($request->hasParameter(JqGridDataHelper::$KEY_SIDX)) {
                $arrParam['orderBy'] = "t1.".$request->getParameter(JqGridDataHelper::$KEY_SIDX)." ".$request->getParameter(JqGridDataHelper::$KEY_SORD);
            }

            $resultSet = $dbTable1->getCustomList($arrParam, $request->getParameter(JqGridDataHelper::$KEY_PAGE), $request->getParameter(JqGridDataHelper::$KEY_ROWS));
        }
        else {
            $resultSet = $dbTable1->getCommonList($request->getParameter(JqGridDataHelper::$KEY_PAGE), $request->getParameter(JqGridDataHelper::$KEY_ROWS));
        }

        $pager     = $dbTable1->getPager();
        $resultSet = $jqGridDataHelper->convert($resultSet, $pager, JsCommonData::getSuccessDatum());

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
   *  do operation according to the oper parameter
    */
    public function executeDoOperation(sfWebRequest $request) {
        // forward to 404 if no oper
        $this->forward404Unless($request->hasParameter(JqGridDataHelper::$KEY_OPER));

        switch($request->getParameter(JqGridDataHelper::$KEY_OPER)) {
            case JqGridDataHelper::$OPER_ADD:
                return $this->executeAdd($request);
                break;
            case JqGridDataHelper::$OPER_EDIT:
                return $this->executeEdit($request);
                break;
            case JqGridDataHelper::$OPER_DEL:
                return $this->executeDelete($request);
                break;
            default:
                $resultSet = JqGridDataHelper::getInstance()->getCommonDatum();
                $resultSet[JqGridDataHelper::$KEY_USER_DATA] = JsCommonData::getErrorDatum();
                return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
                break;
        }
    }

    /*
   * @return inspect result
    */
    protected function inspectDataValidation(sfWebRequest $request, $params=array()) {
        $result    = JsCommonData::getSuccessDatum();
        $reqParams = $request->getParameterHolder()->getAll();
        $dbTable1  = Doctrine::getTable(WacTable::$axis);

        $id = ($reqParams['id']!=JqGridDataHelper::$KEY_EMPTY) ? $reqParams['id'] : 0;
        if($dbTable1->isExistedCode($reqParams['code'], $id)) {
            $result = JsCommonData::getErrorDatum(WacErrorCode::getInfo(WacErrorCode::$duplicatedName, $reqParams['code']), WacErrorCode::$duplicatedName);
            return $result;
        }

        if($dbTable1->isExistedName($reqParams['name'], $id)) {
            $result = JsCommonData::getErrorDatum(WacErrorCode::getInfo(WacErrorCode::$duplicatedName, $reqParams['name']), WacErrorCode::$duplicatedName);
            return $result;
        }
        return $result;
    }

    /*
   * @return validate
    */
    public function executeValidate(sfWebRequest $request) {
        if ($this->getRequest()->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $resultSet = JqGridDataHelper::getInstance()->getCommonDatum();
        $inspectResult = $this->inspectDataValidation($request);
        $resultSet[JqGridDataHelper::$KEY_USER_DATA] = $inspectResult;
        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
   * @return list data array
    */
    public function executeAdd(sfWebRequest $request) {
        if ($this->getRequest()->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $inspectResult = $this->inspectDataValidation($request);
        if($inspectResult['status'] == WacOperationStatus::$Error) {
            $resultSet = JqGridDataHelper::getInstance()->getCommonDatum();
            $resultSet[JqGridDataHelper::$KEY_USER_DATA] = $inspectResult;
        }
        else {
            $dbTable1     = Doctrine::getTable(WacTable::$axis);
            $exceptFields = array("id");
            $reqParams       = $this->getRequest()->getParameterHolder()->getAll();
            $targetItem   = $dbTable1->create();

            $resultSet = JqGridDataHelper::getInstance()->getCommonDatum();
            $resultSet[JqGridDataHelper::$KEY_USER_DATA] = JsCommonData::getSuccessDatum();

            if(count($reqParams)>0) {
                foreach($reqParams as $key => $value) {
                    if($dbTable1->hasColumn($key) and !in_array($key, $exceptFields)) {
                        $accessor = "set".ucfirst($key);
                        $targetItem->$accessor($value);
                    }
                }
                $targetItem->save();
            }
        }

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);

    }

    /*
   * @return process result data array
    */
    public function executeEdit(sfWebRequest $request) {
        // forward to 404 if no id
        $this->forward404Unless($request->hasParameter('id'));

        if ($this->getRequest()->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $inspectResult = $this->inspectDataValidation($request);
        if($inspectResult['status'] == WacOperationStatus::$Error) {
            $resultSet = JqGridDataHelper::getInstance()->getCommonDatum();
            $resultSet[JqGridDataHelper::$KEY_USER_DATA] = $inspectResult;
        }
        else {
            $dbTable1 = Doctrine::getTable(WacTable::$axis);
            $exceptFields = array("id");
            $reqParams = $this->getRequest()->getParameterHolder()->getAll();
            $targetItem = $dbTable1->findOneById($request->getParameter('id'));

            $resultSet = JqGridDataHelper::getInstance()->getCommonDatum();
            $resultSet[JqGridDataHelper::$KEY_USER_DATA] = JsCommonData::getSuccessDatum();

            if(count($reqParams)>0) {
                foreach($reqParams as $key => $value) {
                    if($dbTable1->hasColumn($key) and !in_array($key, $exceptFields)) {
                        $accessor = "set".ucfirst($key);
                        $targetItem->$accessor($value);
                    }
                }
                $targetItem->save();
            }
        }
        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
   * @return process result data array
    */
    public function executeDelete(sfWebRequest $request) {
        // forward to 404 if no id
        $this->forward404Unless($request->hasParameter('id'));

        if ($this->getRequest()->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

//      $resultSet = $this->getRequest()->getParameterHolder()->getAll();
        $resultSet = JqGridDataHelper::getInstance()->getCommonDatum();

        $dbTable1 = Doctrine::getTable(WacTable::$axis);

        $ids = explode(',', $request->getParameter('id'));
        if(count($ids)>0) {
            foreach($ids as $id) {
                $targetItem = $dbTable1->findOneById($id);
                if($targetItem->delete()) {
                    $resultSet[JqGridDataHelper::$KEY_USER_DATA] = JsCommonData::getSuccessDatum();
                }
                else {
                    $resultSet[JqGridDataHelper::$KEY_USER_DATA] = JsCommonData::getErrorDatum(WacErrorCode::getInfo(WacErrorCode::$deleteError), WacErrorCode::$deleteError);
                    break;
                }
            }

        }

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

}
